# -*- coding: utf-8 -*-

# __date:       2021/7/14
# __author:     Yang Chao
# __function:   Merge Two Sorted Lists 合并两个有序链表


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:

    def mergeTwoLists1(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 方法一： 辅助列表重新生成列表
        temp = []
        while l1 or l2:
            if l1 is None:
                temp.append(l2.val)
                l2 = l2.next
            elif l2 is None:
                temp.append(l1.val)
                l1 = l1.next
            elif l1.val <= l2.val:
                temp.append(l1.val)
                l1 = l1.next
            else:
                temp.append(l2.val)
                l2 = l2.next

        dummy = ListNode(-1)
        t = dummy
        for num in temp:
            t.next = ListNode(num)
            t = t.next
        return dummy.next

    def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:
        # 可以直接通过dummy指针方法，加一个nums太麻烦
        dummy = ListNode(-1)
        temp = dummy
        while l1 or l2:
            if l2 is None:
                temp.next = ListNode(l1.val)
                l1 = l1.next
            elif l1 is None:
                temp.next = ListNode(l2.val)
                l2 = l2.next
            elif l1.val <= l2.val:
                temp.next = ListNode(l1.val)
                l1 = l1.next
            else:
                temp.next = ListNode(l2.val)
                l2 = l2.next
            temp = temp.next

        return dummy.next
